<?php
/**
 * 
 * @author luhaoz-work
 *
 */
class LoginFilters extends CFilter{
    protected $_filterChain;
    public $rules = array(
        'myaccount' =>  array(
            'action'        =>  '*',
        )
    );
    
    
    protected function preFilter($filterChain){
        $this->_filterChain = $filterChain;        
        if (array_key_exists($this->_filterChain->controller->getId(),$this->rules)){
            
            if (!array_key_exists('action',$this->rules[$this->_filterChain->controller->getId()])){
                return $this->filterValid();
            }
            
            if (is_string($this->rules[$this->_filterChain->controller->getId()]['action'])){               
                if ($this->rules[$this->_filterChain->controller->getId()]['action'] == '*'){
                    return $this->filterValid();
                }elseif ($this->rules[$this->_filterChain->controller->getId()]['action'] == $this->_filterChain->action->getId()){
                    return $this->filterValid();
                }
            }
            
            if (is_array($this->rules[$controllerId]['action']) && in_array($this->_filterChain->action->getId(),$this->rules[$controllerId]['action'])){
                return $this->filterValid();
            }
          
        }
        return true;
    }
    
    public function filterValid(){
        $isGuest = Yii::app()->user->isGuest;
        if($isGuest){
             Yii::app()->getRequest()->redirect(Yii::app()->baseUrl.Yii::app()->user->loginUrl);
             
        }
        return !$isGuest;
    }
    
}

